About 3783 letters
About 19 minutes
print("hello world") # display "hello world"
Create a text file named code.txt
, open it, write the following code in it and save it:
print("hello world") # display "hello world"
print
is a built-in function that can be used directly without importing modules. It means "print", which usually means outputting content to the command line in programming languages. "hello world"
is the content to be printed, which is wrapped in double quotes to represent a string literal
, which is not discussed here. # display "hello world"
Content starting with #
is a Comment, which is used to explain the function of the code and will be ignored during execution. Open PowerShell, use the cd
command to switch to the directory where code.txt
is located, and enter python code.txt
to run the code:
The above steps show that a Python code file is just an ordinary text file, and running Python code is to let python
read the code file and then perform corresponding operations based on the content.
Usually, Python code files use .py
as the extension, and the entry file is usually named main.py
.
Rename code.txt
to main.py
and run the code with the python main.py
command.
From now on, when creating Python code files, use .py
as the extension.
Usually add #!/usr/bin/env python3
to the beginning of Python code, for example:
#!/usr/bin/env python3
print("hello world")
#!
is called "Shebang", a special mark in Unix-like systems. It is written at the beginning of the file and is used to tell the system how to run the file when it is run directly.
After adding this line, you can run ./main.py
directly without typing python main.py
to run the program.
In Unix-like systems, adding
#!/usr/bin/env python3
to the beginning of the file and then running./main.py
directly is equivalent to executing/usr/bin/env python3 ./main.py
. However,#!
will not search for environment variables, so you must use the full path and cannot write#!python3
directly. The function of/usr/bin/env
is to search for environment variables to run the following commands./usr/bin/env python3
runspython3
in PATH.
Windows system executes files according to their extensions, and
#!
will be ignored as comments.
Editing code with Notepad is very inefficient, and even after renaming the file to main.py
, opening it with Notepad becomes cumbersome.
It is recommended to use Visual Studio Code
(hereinafter referred to as VS Code) to edit the code.
Download VS Code from the Visual Studio Code official website and install it:
The download and installation methods for other operating systems are the same. The website will display the corresponding download link based on the operating system you are using.
Create a folder as your working directory, right-click the folder, and select Open with Code
:
Creating code files, editing code files, and running code can all be done in VS Code.
The shell interface for running the code below is opened by the shortcut Ctrl
+ `
(the key to the left of the number key 1
).
To facilitate development, it is recommended to install this plugin:
Created in 5/15/2025
Updated in 5/15/2025